#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef struct {
	int* arr;
	int head;
	int tail;
	int size;
	int capacity;
} MyCircularQueue;
MyCircularQueue* myCircularQueueCreate(int k) {
	MyCircularQueue* new = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
	if (new == NULL) {
		printf("malloc failed!");
		exit(-1);
	}
	new->head = new->tail = 0;
	new->size = 0;
	new->arr = (int*)malloc(sizeof(int) * k);
	if (new->arr == NULL) {
		printf("malloc failed!");
		exit(-1);
	}
	new->capacity = k;

	return new;
}
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
	assert(obj);
	if (obj->size == 0)
		return true;
	else
		return false;
}

bool myCircularQueueIsFull(MyCircularQueue* obj) {
	assert(obj);
	if (obj->size == obj->capacity)
		return true;
	else
		return false;
}
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
	assert(obj);
	if (myCircularQueueIsFull(obj))
		return false;
	if (myCircularQueueIsEmpty(obj)) {
		obj->arr[obj->tail] = value;
		obj->size++;
		return true;
	}
	else {
		obj->tail++;
		if (obj->tail == obj->capacity)obj->tail = 0;
		obj->arr[obj->tail] = value;
		obj->size++;
		return true;
	}
}

bool myCircularQueueDeQueue(MyCircularQueue* obj) {
	assert(obj);
	if (myCircularQueueIsEmpty(obj))
		return false;
	else {
		if (obj->head == obj->tail) {
			obj->size--;
			return true;
		}
		obj->head++;
		if (obj->head == obj->capacity)obj->head = 0;
		obj->size--;
		return true;
	}
}

int myCircularQueueFront(MyCircularQueue* obj) {
	assert(obj);
	if (myCircularQueueIsEmpty(obj))
		return -1;
	else {
		return obj->arr[obj->head];
	}
}

int myCircularQueueRear(MyCircularQueue* obj) {
	assert(obj);
	if (myCircularQueueIsEmpty(obj))
		return -1;
	else {
		return obj->arr[obj->tail];
	}
}



void myCircularQueueFree(MyCircularQueue* obj) {
	assert(obj);
	free(obj->arr);
	free(obj);
}
int main() {
	MyCircularQueue* q=myCircularQueueCreate(2);
	myCircularQueueEnQueue(q, 1);
	myCircularQueueEnQueue(q, 2);
	myCircularQueueDeQueue(q);
	myCircularQueueEnQueue(q, 3);
	myCircularQueueDeQueue(q);
	myCircularQueueEnQueue(q, 3);
	myCircularQueueDeQueue(q);
	myCircularQueueEnQueue(q, 3);
	myCircularQueueDeQueue(q);
	printf("%d", myCircularQueueFront(q));

	return 0;
}